﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using NLog;

namespace Medianamik.Core.Logging
{
    [Filter("httpException")]
    public sealed class HttpExceptionFilter : Filter
    {

        public int Code { get; set; }

        #region Overrides of Filter

        /// <summary>
        ///             Checks whether log event should be logged or not.
        /// </summary>
        /// <param name="logEvent">Log event.</param>
        /// <returns>
        /// <see cref="F:NLog.FilterResult.Ignore" /> - if the log event should be ignored<br /><see cref="F:NLog.FilterResult.Neutral" /> - if the filter doesn't want to decide<br /><see cref="F:NLog.FilterResult.Log" /> - if the log event should be logged<br />
        /// </returns>
        protected override FilterResult Check(LogEventInfo logEvent)
        {
            var exc = logEvent.Exception;
            while (exc != null)
            {
                if (exc is HttpException
                    && ((HttpException)exc).GetHttpCode() == Code)
                {
                    return base.Result;
                   // break;
                }
                exc = exc.InnerException;
            }
            return FilterResult.Neutral;
        }

        #endregion
    }
}
